#include "server.h"

#define ARRAYSIZE	100
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define BUFFSIZE        1024

int server_poll_create(int fd)
{
	sqlite3         		*db;
	int             		array_full = 0;
	struct pollfd 			fd_array[ARRAYSIZE];
	nfds_t 				fd_num = 0;
	int 				re_val = 0;
	int 				i;
	int 				clienfd = -1;
	struct sockaddr_in 		client_addr;
	socklen_t 			client_addr_len;	
	int				maxfd;

	char            		receive_buff[BUFFSIZE];
	char            		finish[25]="serve receive finish!";
	char            		ID[50];
	char            		temp[10];
	char            		time[20];


	/*初始化poll_struct*/
	for(i = 0; i < ARRAY_SIZE(fd_array); i++)
	{
		fd_array[i].fd = -1;
	}
	fd_array[0].fd 		= fd;
	fd_array[0].events 	= POLLIN;
	maxfd			= 0;

	while(1)
	{
		//printf("server is waitting for new client or data packets...\n");
		re_val = poll(fd_array,maxfd + 1,-1);//阻塞不超时
		if(re_val < 0)
		{
			printf("sere_valer socket poll error:%s\n",strerror(errno));
			return -1;
		}
		else if(0 == re_val)
		{
			printf("poll get timeout!\n");
			continue;
		}
		else
		{

			for(i = 0; i < ARRAY_SIZE(fd_array); i++)
			{

				if( fd_array[i].fd < 0 )
				{
					continue;
				}

				/*判断是否是新的套接字(客户端)连接过来*/
				if(0 == i && fd_array[0].revents & POLLIN)
				{
					clienfd = accept(fd_array[0].fd,(struct sockaddr *)&client_addr,&client_addr_len);
					if(clienfd < 0)
					{
						printf("sere_valer poll accept error:%s\n",strerror(errno));
						continue;
					}

					array_full = 0;


					/*将连接描述符添加到pollfd_arrsy中，以便监测*/
					for(i = 1;i < ARRAY_SIZE(fd_array); i++)
					{
						if(fd_array[i].fd < 0)
						{

							printf("accept new client[%d] and add it into array\n", clienfd );
							mylog(__FUNCTION__,__LINE__,INFO,"accept new client[%d] and add it into array\n",clienfd);
							fd_array[i].fd = clienfd;
							fd_array[i].events = POLLIN|POLLOUT;
							array_full = 1;
							break;
						}
					}

					//判断数组是否为满
					if(!array_full)
					{

						printf("accept new client[%d] but full, so refuse it\n", clienfd);
						close(clienfd);
						continue;
					}

					//新客户端连接之后重新计算maxfd
					maxfd = i > maxfd ? i : maxfd;


				}

				else if(fd_array[i].revents & POLLIN)
				{

					//开始读取数据
					memset(receive_buff,0,sizeof(receive_buff));

					if( (re_val = read(fd_array[i].fd, receive_buff, sizeof(receive_buff))) <= 0)
					{
						printf("server  read from client[%d] failure or get disconncet.\n", fd_array[i].fd);
						close(fd_array[i].fd);
						fd_array[i].fd = -1;
					}
					else
					{
						printf("server read [%d] bytes from clienfd[%d]:%s\n",re_val,fd_array[i].fd,receive_buff);
						mylog(__FUNCTION__,__LINE__,INFO,"server read [%d] bytes from clienfd[%d]:%s\n",\
								re_val,fd_array[i].fd,receive_buff);

						memset(ID,0,sizeof(ID));
						memset(temp,0,sizeof(temp));
						memset(time,0,sizeof(time));

						deal_data(receive_buff,ID,temp,time);//数据包解析

						datbas_open(&db);//打开数据库

						datbas_insert(db,ID,temp,time);//插入数据库

						datbas_close(db);//关闭数据库

						if( write(fd_array[i].fd,finish,sizeof(finish)) < 0 )
						{
							printf("server  write to client[%d] failure: %s\n", fd_array[i], strerror(errno));
							close(fd_array[i].fd);
							fd_array[i].fd = -1;
						}
					}

				}


			}

		}
	}


}

